package cn.lhl.web.baseinformation;

import cn.lhl.domain.cargo.Factory;
import cn.lhl.domain.cargo.FactoryExample;
import cn.lhl.service.FactoryManagerService;
import cn.lhl.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.UUID;

@RequestMapping("/baseInformation")
@Controller
public class FactoryController extends BaseController {

    @Reference
    private FactoryManagerService factoryManagerService;

    @RequestMapping(value = "/factorylist",name = "分页显示所有厂家列表")
    public String factoryList(@RequestParam(defaultValue = "1")int page,
                              @RequestParam(defaultValue = "5")int size){
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria feCriteria = factoryExample.createCriteria();
        feCriteria.andCtypeIsNotNull();

        PageInfo pageInfo = factoryManagerService.findAll(factoryExample, page, size);
        model.addAttribute("page",pageInfo);
        return "baseinformation/factory-list";
    }

    @RequestMapping(value = "/toAdd",name = "去新增界面")
    public String toAdd(){
        return "/baseinformation/factory-add";
    }

    @RequestMapping(value = "/add",name = "新增")
    public String add(Factory factory){
        //增加ID
        factory.setCompanyId(companyId);
        factory.setCompanyName(companyName);

        //默认状态未启用
        factory.setState(1);

        factory.setCreateTime(new Date());
        factory.setId(UUID.randomUUID().toString());
        factoryManagerService.add(factory);
        return "redirect:/baseInformation/factorylist.do";
    }

    @RequestMapping(value = "/delete",name = "对厂家信息进行删除")
    public String delete(String id){
        factoryManagerService.delete(id);
        return "redirect:/baseInformation/factorylist.do";
    }

   //去编辑界面
    @RequestMapping(value = "/toUpdate",name = "对厂家信息进行修改")
    public String toUpdate(String id){
        //根据id获取厂家信息
        Factory factory = factoryManagerService.findById(id);
        request.setAttribute("factory",factory);
        return "/baseinformation/factory-update";
    }

    @RequestMapping(value = "/update",name = "对厂家信息进行修改")
    public String update(Factory factory){
        factory.setUpdateTime(new Date());
        factoryManagerService.update(factory);
        return "redirect:/baseInformation/factorylist.do";
    }

    @RequestMapping(value = "/submit",name = "对厂家货物状态进行启用")
    public String startState(String id){
        Factory factory = factoryManagerService.findById(id);
        factory.setState(1);
        factoryManagerService.update(factory);
        return "redirect:/baseInformation/factorylist.do";
    }

    @RequestMapping(value = "/cancel",name = "对厂家货物状态进行停用")
    public String downState(String id){
        Factory factory = factoryManagerService.findById(id);
        factory.setState(0);
        factoryManagerService.update(factory);
        return "redirect:/baseInformation/factorylist.do";
    }
}
